﻿using Kalman;
using NLog;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Vod;
using TencentCloud.Vod.V20180717;
using TencentCloud.Vod.V20180717.Models;
using VodSDK;

namespace TCloud
{

    /// <summary>
    /// 腾讯云云点播功能：https://cloud.tencent.com/document/product/266
    /// </summary>
    public class TCloudVod : SingletonBase<TCloudVod>
    {
        readonly ILogger log;
        readonly string secretId;
        readonly string secretKey;
        readonly string region = "ap-shanghai"; //可选区域：ap-beijing,ap-nanjing,ap-guangzhou
        VodClient client;
        VodUploadClient uploadClient;

        public TCloudVod() : base()
        {
            log = LogManager.GetLogger("TCloud");

            var options = AppConfig.Instance.GetSection<TCloudOptions>("TCloud");
            if (options != null)
            {
                secretId = options.SecretId;
                secretKey = options.SecretKey;
            }
            else
            {
                secretId = "AKIDe8MJ0QZS5RWKr87pGQJvGL9furYy81e0"; 
                secretKey = "6vUllUv5yavzmIWUBbkdFaeQwB9qHRKw"; 
            }

            //其他视频管理功能，这个SDK的上传功能比较麻烦
            client = new VodClient(new Credential { SecretId = secretId, SecretKey = secretKey }, region); 

            //专用于视频上传
            uploadClient = new VodUploadClient(secretId, secretKey); 
        }

        
        /// <summary>
        /// 上传视频
        /// </summary>
        public void Upload()
        {
            //https://cloud.tencent.com/document/product/266/37896
            var request = new VodUploadRequest
            {
                  
            };

            var response = uploadClient.Upload(region, request);

        }

        #region 获取客户端上传签名

        //生成签名：https://cloud.tencent.com/document/product/266/9221
        //小程序上传：https://cloud.tencent.com/document/product/266/18177

        private long GetTimeStamp()
        {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
            return Convert.ToInt64(ts.TotalSeconds);
        }
        private byte[] hash_hmac_byte(string signString, string secretKey)
        {
            var enc = Encoding.UTF8; 
            var hmac = new HMACSHA1(enc.GetBytes(secretKey));
            hmac.Initialize();
            byte[] buffer = enc.GetBytes(signString);
            return hmac.ComputeHash(buffer);
        }

        public string GetUploadSign()
        {
            var ts = GetTimeStamp();
            var expired = 3600 * 24 * 2;
            var random = RandomUtil.GetInteger(0, 1000000);
            var text = $"secretId={Uri.EscapeDataString(secretId)}&currentTimeStamp={ts}&expireTime={ts + expired}&random={random}";

            byte[] bytesSign = hash_hmac_byte(text, secretKey);
            byte[] byteContent = System.Text.Encoding.Default.GetBytes(text);
            byte[] nCon = new byte[bytesSign.Length + byteContent.Length];
            bytesSign.CopyTo(nCon, 0);
            byteContent.CopyTo(nCon, bytesSign.Length);
            return Convert.ToBase64String(nCon);
        }
        #endregion

    }
}
